package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsBuyQualityProDao;
import com.sugon.entity.SysMsgEntity;
import com.sugon.entity.ToolsBuyQualityProEntity;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsBuyQualityProService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 外购工装质量问题Service实现类
 *
 * @author sugon
 * @date 2019-08-16 09:20:49
 */
@Service("toolsBuyQualityProService")
public class ToolsBuyQualityProServiceImpl implements ToolsBuyQualityProService {
    @Autowired
    private ToolsBuyQualityProDao toolsBuyQualityProDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public ToolsBuyQualityProEntity queryObject(String id) {
        return toolsBuyQualityProDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<ToolsBuyQualityProEntity> queryList(Map<String, Object> map) {
        return toolsBuyQualityProDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsBuyQualityProDao.queryTotal(map);
    }

    /**
     * @param toolsBuyQualityPro 实体
     * @return
     */
    @Override
    public int save(ToolsBuyQualityProEntity toolsBuyQualityPro) {
        toolsBuyQualityPro.setId(RandomUtil.randomString(32));
        toolsBuyQualityPro.setCreateTime(new Date());
        toolsBuyQualityPro.setCreateUser(getUserId());
        toolsBuyQualityPro.setApprovalUser(getUserId());
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(toolsBuyQualityPro.getId(), ProcessInfoEnum.TLS_WGGZLZ, 0, null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        String Id = toolsBuyQualityPro.getId();
        sysMsgEntity.setDataId(Id);
        sysMsgEntity.setDataUrl("../tools/toolsbuyqualitypro.html?Id=" + Id);
        sysMsgService.addApproveItem(ProcessInfoEnum.TLS_WGGZLZ, sysMsgEntity, 0);
        return toolsBuyQualityProDao.save(toolsBuyQualityPro);
    }

    /**
     * @param toolsBuyQualityPro 实体
     * @return
     */
    @Override
    public int update(ToolsBuyQualityProEntity toolsBuyQualityPro) {
        toolsBuyQualityPro.setUpdateTime(new Date());
        toolsBuyQualityPro.setUpdateUser(getUserId());
        int rows = toolsBuyQualityProDao.update(toolsBuyQualityPro);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = toolsBuyQualityPro.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../tools/toolsbuyqualitypro.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.TLS_WGGZLZ, msgEntity, 0);
        }
        return rows;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return toolsBuyQualityProDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return toolsBuyQualityProDao.deleteBatch(ids);
    }

    /**
     * @param toolsBuyQualityPro
     * @return
     */
    @Override
    public int review(ToolsBuyQualityProEntity toolsBuyQualityPro) {
        toolsBuyQualityPro.setApprovalTime(new Date());
        toolsBuyQualityPro.setApprovalUser(getUserId());
        sysProcessInfoService.saveProcessInfo(toolsBuyQualityPro.getId(), ProcessInfoEnum.TLS_WGGZLZ, Integer.parseInt(toolsBuyQualityPro.getApprovalStatus()), null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        String Id = toolsBuyQualityPro.getId();
        sysMsgEntity.setDataId(Id);
        sysMsgEntity.setDataType(ProcessInfoEnum.TLS_WGGZLZ);
        sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        sysMsgService.allMsgData(sysMsgEntity);
        return toolsBuyQualityProDao.update(toolsBuyQualityPro);
    }
}
